//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Modules.Console.Components;
using DotNetNuke.Services.Localization;
namespace DotNetNuke.Modules.Admin.Console
{
	partial class Settings : Entities.Modules.ModuleSettingsBase
	{
		public override void LoadSettings()
		{
			try {
				if (Page.IsPostBack == false) {
					List<TabInfo> portalTabs = TabController.GetPortalTabs(PortalId, DotNetNuke.Common.Utilities.Null.NullInteger, false, true);
					if (UserInfo != null && UserInfo.IsSuperUser) {
						TabCollection hostTabs = new TabController().GetTabsByPortal(Null.NullInteger);
						portalTabs.AddRange(hostTabs.Values);
					}
					ParentTab.Items.Clear();
					foreach (TabInfo t in portalTabs) {
						if ((Security.Permissions.TabPermissionController.CanViewPage(t))) {
							ParentTab.Items.Add(new ListItem(t.IndentedTabName, t.TabID.ToString()));
						}
					}
					ParentTab.Items.Insert(0, "");
					SelectDropDownListItem(ref ParentTab, "ParentTabID");
					foreach (string val in ConsoleController.GetSizeValues()) {
						DefaultSize.Items.Add(new ListItem(Localization.GetString(val, base.LocalResourceFile), val));
					}
					SelectDropDownListItem(ref DefaultSize, "DefaultSize");
					if (Settings.ContainsKey("AllowSizeChange")) {
						AllowResize.Checked = Convert.ToBoolean(Settings["AllowSizeChange"]);
					}
					foreach (string val in ConsoleController.GetViewValues()) {
						DefaultView.Items.Add(new ListItem(Localization.GetString(val, base.LocalResourceFile), val));
					}
					SelectDropDownListItem(ref DefaultView, "DefaultView");
					if (Settings.ContainsKey("AllowViewChange")) {
						AllowViewChange.Checked = Convert.ToBoolean(Settings["AllowViewChange"]);
					}
					if (Settings.ContainsKey("ShowTooltip")) {
						ShowTooltip.Checked = Convert.ToBoolean(Settings["ShowTooltip"]);
					}
					if (Settings.ContainsKey("ConsoleWidth")) {
						ConsoleWidth.Text = Convert.ToString(Settings["ConsoleWidth"]);
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		public override void UpdateSettings()
		{
			try {
				Entities.Modules.ModuleController objModules = new Entities.Modules.ModuleController();
				string wdth = string.Empty;
				if ((ConsoleWidth.Text.Trim().Length > 0)) {
					try {
						wdth = Unit.Parse(ConsoleWidth.Text.Trim()).ToString();
					} catch (Exception ex) {
						throw new Exception("ConsoleWidth value is invalid. Value must be numeric.");
					}
				}
				if ((ParentTab.SelectedValue == string.Empty)) {
					objModules.DeleteModuleSetting(ModuleId, "ParentTabID");
				} else {
					objModules.UpdateModuleSetting(ModuleId, "ParentTabID", ParentTab.SelectedValue);
				}
				objModules.UpdateModuleSetting(ModuleId, "DefaultSize", DefaultSize.SelectedValue);
				objModules.UpdateModuleSetting(ModuleId, "AllowSizeChange", AllowResize.Checked.ToString());
				objModules.UpdateModuleSetting(ModuleId, "DefaultView", DefaultView.SelectedValue);
				objModules.UpdateModuleSetting(ModuleId, "AllowViewChange", AllowViewChange.Checked.ToString());
				objModules.UpdateModuleSetting(ModuleId, "ShowTooltip", ShowTooltip.Checked.ToString());
				objModules.UpdateModuleSetting(ModuleId, "ConsoleWidth", wdth);
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void SelectDropDownListItem(ref DropDownList ddl, string key)
		{
			if (Settings.ContainsKey(key)) {
				ddl.ClearSelection();
				ListItem selItem;
				selItem = ddl.Items.FindByValue(Convert.ToString(Settings[key]));
				if (selItem != null) {
					selItem.Selected = true;
				}
			}
		}
	}
}
